package co.baiku.boot.weixin;

import co.baiku.boot.common.message.Message;
import co.baiku.boot.common.tools.HttpTools;
import co.baiku.boot.common.tools.JsonTools;
import co.baiku.boot.config.WeixinSnsConfig;
import co.baiku.boot.message.WeixinSnsAccessToken;
import co.baiku.boot.message.WeixinUserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 微信网页授权(sns)模块
 */
@Component
@ConditionalOnProperty("ajavaer.weixin.sns.appid")
public class WeixinSns {

    @Resource
    private WeixinSnsConfig snsConfig;
    private Logger log = LoggerFactory.getLogger(WeixinSns.class);

    public WeixinSns(WeixinSnsConfig config) {
        this.snsConfig = config;
    }

    /**
     * 获得授权token
     *
     * @param code
     * @return
     */
    public Message<WeixinSnsAccessToken> getAccessToken(String code) {
        String url = snsConfig.getSns_access_token_url(code);
        Message<WeixinSnsAccessToken> accessTokenResult = HttpTools.get(url, WeixinSnsAccessToken.class);
        log.debug("Get SNS Access Token From Wx,url:{},res:{}", url, JsonTools.beanToJson(accessTokenResult));
        if (accessTokenResult.isSuccess()) {
            WeixinSnsAccessToken resultData = accessTokenResult.getResult();
            if (resultData.success()) {
                return accessTokenResult;
            } else {
                log.error("weixin error:{}", JsonTools.beanToJson(resultData));
                return Message.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), "get AccessToken error:" + resultData.getErrmsg());
            }
        } else {
            log.error("connect weixin server error:{}", accessTokenResult.getMsg());
            return Message.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), accessTokenResult.getMsg());
        }
    }

    /**
     * 获得用户信息
     *
     * @param accessToken
     * @param openid
     * @return
     */
    public Message<WeixinUserInfo> getUserInfo(String accessToken, String openid) {
        String url = snsConfig.getUserinfo_url(accessToken, openid);
        Message<WeixinUserInfo> weixinUserInfoMessage = HttpTools.get(url, WeixinUserInfo.class);
        if (weixinUserInfoMessage.isSuccess()) {
            WeixinUserInfo resultData = weixinUserInfoMessage.getResult();
            if (resultData.success()) {
                return weixinUserInfoMessage;
            } else {
                log.error("weixin error:{}", JsonTools.beanToJson(resultData));
                return Message.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), "get UserInfo error:" + resultData.getErrmsg());
            }
        } else {
            log.error("connect weixin server error:{}", weixinUserInfoMessage.getMsg());
            return Message.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), weixinUserInfoMessage.getMsg());
        }
    }

    public String base(String redirect_uri, String state) {
        return snsConfig.getSns_code_url(redirect_uri, "snsapi_base", state);
    }

    public String userinfo(String redirect_uri, String state) {
        return snsConfig.getSns_code_url(redirect_uri, "snsapi_userinfo", state);
    }


    public String webSiteUserinfo(String redirect_uri, String state) {
        return snsConfig.getSns_website_code_url(redirect_uri, state);
    }

}
